package com.lanou.dllo.soccernews.fragment.datasfragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.adapter.ShooterAdapter;
import com.lanou.dllo.soccernews.bean.ShooterBean;
import com.lanou.dllo.soccernews.volley.VolleySingleton;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/11/4.
 */
public class ShooterFragment extends Fragment {

    private ListView listView;
    private ShooterAdapter adapter;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.i("lanou", "快快快快快");
        return inflater.inflate(R.layout.fragment_shooter, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = (
                ListView) view.findViewById(R.id.shoot_list);
        adapter = new ShooterAdapter(getActivity());
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getData();


    }

    private void getData() {

        String url = "https://www.dongqiudi.com/data/goal_ranking/8?competitionID=8";

        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        //   CentBean centBean = gson.fromJson(response, CentBean.class);

                        List<ShooterBean> centBeans = gson.fromJson(response, new TypeToken<List<ShooterBean>>() {
                        }.getType());
                      //  Log.i("lanou", centBeans + "ddddddddddd");


                        if (centBeans != null && centBeans.size() > 0) {
                            adapter.addDatas(centBeans);
                            listView.setAdapter(adapter);
                        }


                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // TODO Auto-generated method stub
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        volleySingleton.addRequest(request);
    }


}
